﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHospital.BLL;
namespace MyHospital.web
{
    public partial class BookEnd : System.Web.UI.Page
    {
        BookService bookService = new BookService();
        protected void Page_Load(object sender, EventArgs e)
        {
            int doctorId = 0;
            if (Session["doctorId"] != null)
            {
                doctorId = (int)(Session["doctorId"]);
            }
            String date = (String)Session["date"];
            DateTime d1 = Convert.ToDateTime(date);
            GridView1.DataSource = bookService.doctorShowOfId(doctorId);
            GridView1.DataBind();
            GridView2.DataSource = bookService.doctorTimeShow(doctorId, d1);
            GridView2.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow row = btn.Parent.Parent as GridViewRow;
            String time = row.Cells[0].Text;//获得time
            String date = (String)Session["date"];
            String[] times = time.Split('-');
            String dateNow = DateTime.Now.ToShortDateString().ToString();
            DateTime d1 = Convert.ToDateTime(date);
            DateTime d2 = Convert.ToDateTime(dateNow);
            if (DateTime.Compare(d1, d2) == 0)
            {
                String timeNow = DateTime.Now.ToShortTimeString().ToString();
                DateTime dt1 = Convert.ToDateTime(times[0]);//就诊时间段的开始
                DateTime dt2 = Convert.ToDateTime(timeNow);//现在时间
                if (DateTime.Compare(dt1, dt2) > 0)
                {
                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                    TimeSpan ts = ts2.Subtract(ts1).Duration();

                    //距离诊断的时间
                    if (ts.Hours < 1) //距离诊断的时间小于1小时
                    {
                        Response.Write("<script>alert('请提前一个小时预约！');window.location.href='BookEnd.aspx';</script>");
                    }
                    else
                    {
                        //String date = (String)Session["date"];
                        int patId = (int)Session["PatientID"];
                        int doctorId = (int)(Session["doctorId"]);
                        if (bookService.bookUpdate(doctorId, time))
                        {
                            bookService.insertBookInfor(patId, doctorId, time, date);
                            Response.Write("<script>alert('预约成功！');window.location.href='BookEnd.aspx';</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('该时段预约已满！');window.location.href='BookEnd.aspx';</script>");
                        }
                    }
                }
                else
                {
                    Response.Write("<script>alert('该时段已无法预约！');window.location.href='BookEnd.aspx';</script>");
                }
            }
            else
            {
                int patId = (int)Session["PatientID"];
                int doctorId = (int)(Session["doctorId"]);
                if (bookService.bookUpdate(doctorId, time))
                {
                    bookService.insertBookInfor(patId, doctorId, time, date);
                    Response.Write("<script>alert('预约成功！');window.location.href='BookEnd.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('该时段预约已满！');window.location.href='BookEnd.aspx';</script>");
                }
            }

        }
    }
}